import { createApp } from 'vue'
// import { createPinia } from 'pinia'
import './style.css'
import App from './App.vue'
import router from './router'

import pinia from './stores/index'

// 路由守卫（在路由跳转前执行）
router.beforeEach((to, from) => {
  const token = localStorage.getItem('token')
  // 非登陆页面，且token不存在，跳转到登陆页面
  if(!token && to.path !== '/login'){
    return '/login'
    // router.push('/login')
  }else if(token && to.path === '/login'){
    return '/'
  }else{
    return true
  }
})

import { useCounterStore } from './stores/menu'






// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// const pinia = createPinia()


// pinia挂载
app.use(pinia)


const menu = useCounterStore()
// 刷新后的动态路由添加， getItem获取的是字符串，需要转换成对象, (需要放在路由挂载之前)

const localDate = localStorage.getItem('v3pz')
// const parsedData = JSON.parse(localDate);
// console.log(parsedData, 'localDate');

if(localDate){
  
  
  menu.dynamicMenu(JSON.parse(localDate).routerList)
  // console.log('localDate', parsedData.routerList);
  
  console.log('zhixing');
  
  menu.routerList.forEach(item => {
    router.addRoute('main', item)
    console.log('item', item );
    
  }
  )
}

// 路由挂载
app.use(router)


app.mount('#app')



